package com.mall.app.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mall.app.bean.TbBuyer;
import com.mall.app.bean.TbCart;
import com.mall.app.bean.TbGoods;
import com.mall.app.service.TbCartService;



/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author LeeSin
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/cart")
public class TbCartController {
	@Autowired
	private TbCartService cartService;
	
	@RequestMapping("/listPage")
	public Map<String,Object> listPage(int page,int limit, int buyerId){
		//设置查询条件
//		QueryWrapper<TbCart> queryWrapper = new QueryWrapper<>();
//		if(cartId!=null){
//			queryWrapper.like(true, "cartId", cartId);
//		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", (page - 1) * limit);
		map.put("size", limit);
		//map.put("cartId", cartId);
		map.put("buyerId", buyerId);
	
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("count", cartService.getCount(map));
		result.put("data", cartService.listPageCart(map));
		return result;
	}
	
	@RequestMapping("/list")
	public List<TbCart> list(Integer buyerId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buyerId", buyerId);
		return cartService.listCart(map);
	}
	
	@RequestMapping("/delete") 					//原本参数类型是List<Integer>，做了修改
	public boolean removeCart(@RequestParam("cartIds[]")String[] cartIds){
		return cartService.removeBatchCarts(cartIds);
	}

	@RequestMapping("/update")
	public boolean updateCart(TbCart cart) {
		return cartService.updateCart(cart);
	}
	
	@RequestMapping("/insert")
	public boolean insertCart(Integer bid,Integer gid,Integer goodsQuantity) {
		TbCart cart = new TbCart();
		cart.setBuyerId(bid);
		TbGoods goods = new TbGoods();
		goods.setGid(gid);
		cart.setGoods(goods);
		
		TbBuyer buyer = new TbBuyer();
		buyer.setBid(bid);
		cart.setBuyerId(bid);
		
		cart.setQuantity(goodsQuantity);
		
		return cartService.insertCart(cart);
	}
}

